package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.TMMcFundInfoMapper;
import cc.rengu.igas.momp.common.entity.TMMcFundInfo;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;

/**
 * 营销活动打款记录表数据库操作接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/7/8 15:59
 */

public class TMMcFundInfoMapperImpl implements TMMcFundInfoMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * 新增单条商户成功打款数据数据库操作接口实现
     *
     * @param tmMcFundInfo 营销活动打款记录表数据库操作实体类
     * @return int 返回
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/7/8 16:00
     */
    @Override
    public int insertSingleRecordForMchntFundInfo(TMMcFundInfo tmMcFundInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertSingleRecordForMchntFundInfo");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmMcFundInfo, TMMcFundInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("插入法人机构号<{}>营销活动编号<{}>的<{}>的打款资金数据失败!<{}>", tmMcFundInfo.getInstId(), tmMcFundInfo.getMcNo(), tmMcFundInfo.getPayerAcctId(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 取当前数据库最大记录顺序号数据库操作接口实现
     * 如果有记录,返回值大于等于0
     * 如果无数据或失败,返回值小于0;
     *
     * @param tmMcFundInfo 营销活动打款记录表数据库操作实体类
     * @return int 当前数据库最大记录顺序号
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/7/8 17:39
     */
    @Override
    public int checkMaxmiumRecordIndexInfo(TMMcFundInfo tmMcFundInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "checkMaxmiumRecordIndexInfo");

        String instId = tmMcFundInfo.getInstId();
        String mcNo = tmMcFundInfo.getMcNo();
        String payerId = tmMcFundInfo.getPayerAcctId();

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmMcFundInfo, tmMcFundInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            if (Database.DBS_NOT_FOUND == iReturnCode || Database.DBS_FETCH_NULL == iReturnCode) {
                return CommonConstant.MINUS_ONE;
            } else {
                rglog.error("插入法人机构号<{}>营销活动编号<{}>的<{}>的打款资金数据失败!<{}>", instId, mcNo, payerId, iReturnCode);
                /* 需要确保返回负数 */
                if (CommonConstant.ZERO >= Integer.signum(iReturnCode)) {
                    return iReturnCode;
                } else {
                    return CommonConstant.MINUS_ONE * iReturnCode;
                }
            }
        }

        return Integer.parseInt(tmMcFundInfo.getRecordIndex());
    }

    @Override
    public int updateTMMcFundInfo(TMMcFundInfo tmMcFundInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateTMMcFundInfo");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmMcFundInfo, TMMcFundInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("插入法人机构号<{}>营销活动编号<{}>的<{}>的打款资金数据失败!<{}>", tmMcFundInfo.getInstId(), tmMcFundInfo.getMcNo(), tmMcFundInfo.getPayerAcctId(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 取当前数据库最大记录顺序号数据库操作接口实现
     * 如果有记录,返回值大于等于0
     * 如果无数据或失败,返回值小于0;
     *
     * @param tmMcFundInfo 营销活动打款记录表数据库操作实体类
     * @return int 当前数据库最大记录顺序号
     * @throws Exception 异常信息
     * @author JL Pang
     * @date 2020/12/2 17:39
     */
    @Override
    public int checkMaxmiumRecordIndexInfoByInstIdMcNo(TMMcFundInfo tmMcFundInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);

        String instId = tmMcFundInfo.getInstId();
        String mcNo = tmMcFundInfo.getMcNo();

        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "checkMaxmiumRecordIndexInfoByInstIdMcNo");
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tmMcFundInfo, TMMcFundInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取法人机构号<{}>的商户<{}>的营销活动<{}>手续费达标统计表信息列表失败!", tmMcFundInfo.getInstId(), tmMcFundInfo.getMcNo());
            return CommonConstant.MINUS_ONE;
        } else {
            return CommonConstant.ONE;
        }
    }
}
